package com.qk.controller;

import com.qk.common.Result;
import com.qk.utils.AliyunOSSOperator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

@Slf4j
@RestController
public class UploadController {

    @Autowired
    private  AliyunOSSOperator aliyunOSSOperator;



    /*@PostMapping("/upload")
        public Result upload(String username, Integer age, MultipartFile file) throws Exception {
            log.info("{},{},{}" + username, age, file);
            // 1.构造一个文件名(不能重复)
            String fileName = file.getOriginalFilename();
            // 2.截取文件名后缀
            String extname = fileName.substring(fileName.lastIndexOf("."));
            // 3.通过uuid生成文件名
            String newFileName = UUID.randomUUID().toString() + extname;
            // 4.保存文件
            file.transferTo(new File("E:\\image\\" + newFileName));
            return Result.success();
        }*/
    @PostMapping("/upload")
    public Result upload(MultipartFile image) throws Exception {
        log.info("文件上传:{}", image);
        String fileName = image.getOriginalFilename();
        // 2.截取文件名后缀
        String extname = fileName.substring(fileName.lastIndexOf("."));
        // 3.通过uuid生成文件名
        String newFileName = UUID.randomUUID().toString() + extname;
        String url = aliyunOSSOperator.upload(image.getBytes() , newFileName);
        return Result.success(url);
    }
}
